<template>
  <el-dialog title="新增员工" :visible.sync="showDialog" >
    <el-form :model="formData" :rules="rules" ref="addEmployee">
      <el-form-item label="姓名" prop="username">
        <el-input
          style="width: 50%"
          placeholder="请输入姓名"
          v-model="formData.username"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          style="width: 50%"
          placeholder="请输入手机号"
          v-model="formData.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          type="date"
          placeholder="请选择入职时间"
          v-model="formData.timeOfEntry"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          placeholder="请选择聘用形式"
          v-model="formData.formOfEmployment"
        >
          <el-option
            v-for="item in EmployeesType.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          style="width: 50%"
          placeholder="请输入工号"
          v-model="formData.workNumber"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          style="width: 50%"
          placeholder="请输入部门"
          v-model="formData.departmentName"
          @focus="getDepartments"
        ></el-input>
        <el-tree
          v-if="showTree"
          :data="treeData"
          v-loading = 'loading'
          default-expand-all
          :props="{ label: 'name' }"
          @node-click= 'selectNode'
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          type="date"
          placeholder="请选择转正时间"
          v-model="formData.correctionTime"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <!-- dialog专门放置底部操作栏的插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 引入聘用形式
import EmployeesType from "@/api/constant/employees";
// 获取树形部门
import { getDepartments } from "@/api/departments";
// depts是一个数组 它需要转化成树形结构 才可以被 el-tree 显示
import { tranListToTreeData } from "@/utils/index";
import {addEmployee} from "@/api/employees"
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      EmployeesType, // 聘用形式的数据
      showTree: false, // 显示隐藏部门树
      loading: false,   //控制树的显示和隐藏的进度条
      treeData: [], // 存放部门数据
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 11,
            message: "姓名应在1-4位",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
    };
  },
  methods: {
    async getDepartments() {
      // 显示树
      this.showTree = true;
      this.loading = true;
      // 获取部门数据
      const { depts } = await getDepartments();
      // depts是一个数组 它需要转化成树形结构 才可以被 el-tree 显示
      this.treeData = tranListToTreeData(depts, "");
      this.loading = false;
    },
    selectNode(node){
      // 点击tree的几点时触发
      this.formData.departmentName = node.name
      // 关闭树
      this.showTree = false
    },
    // 点击确定 新增员工
    async btnOk(){
      try{
        // 先校验整个表单
        await this.$refs.addEmployee.validate();
        // 成功后执行 添加
        await addEmployee(this.formData);
        // 重新渲染
        this.$parent.getEmployeeList()
        // 关闭弹窗 this.$parent === 父组件中的this
        this.$parent.showDialog = false
      }catch(err){
        console.log(err)
      }
    },
    async btnCancel(){
      // 重置弹框数据和 校验结果
      this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      this.$refs.addEmployee.resetFields();
      // this.$parent.showDialog = false;
      this.$emit('update:showDialog', false)
    }
  },
};
</script>

<style scoped>
</style>